/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.czeczek.domain.internal;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;

import com.czeczek.domain.IndicatorDim;
import com.czeczek.domain.IndicatorQuote;
import com.czeczek.domain.Quote;
import com.czeczek.domain.dao.IndicatorFactDao;
import com.czeczek.domain.dao.IndicatorsDao;
import com.czeczek.indicators.Indicator;

/**
 * Delegates the actual writing to custom DAO delegate. Allows configurable
 * exception raising for testing skip and restart.
 */
public class IndicatorWriter implements ItemWriter<Quote> {

	private static Log log = LogFactory.getLog(IndicatorWriter.class);
	
	private List<Indicator> indicators;
	
	//List of quotes per symbol. list contains map of quotes per indicator id
	private Map<String, Map<Long, LinkedList<Quote>>> buffer;

	private IndicatorFactDao indicatorFactDao;
	private IndicatorsDao indicatorDimDao;
	
	public IndicatorWriter() {
		buffer = new HashMap<String, Map<Long,LinkedList<Quote>>>();
	}

	public void write(List<? extends Quote> quotes) {

		for (Quote quote: quotes) {
			try {
				process(quote);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			log.info("process");

			//dao.writeIndicatorFact(indicator);
		}

	}
	
	private Map<Long, IndicatorQuote> process(Quote item) throws Exception {
		Map<Long, IndicatorQuote> result = new HashMap<Long, IndicatorQuote>();
		//Fetch all indicators if there were no retrieved previously
		if (indicators == null || indicators.isEmpty()) {
			indicators = indicatorDimDao.getIndicators();
		}
		LinkedList<Quote> quotesList = null;
		Map<Long, LinkedList<Quote>> symbolQuotes = null;
		IndicatorQuote indicatorQuote = null;
		for(Indicator indicator: indicators) {
			if (buffer.containsKey(item.getSymbol())) {
				symbolQuotes = buffer.get(item.getSymbol());
				if (symbolQuotes.containsKey(indicator.getIndicatorId())) {
					quotesList = symbolQuotes.get(indicator.getIndicatorId());	
				} else {
					quotesList = new LinkedList<Quote>();
					quotesList.add(item);
					symbolQuotes.put(indicator.getIndicatorId(), quotesList);
				}
			} else {
				quotesList = new LinkedList<Quote>();
				quotesList.add(item);
				symbolQuotes = new HashMap<Long, LinkedList<Quote>>();
				symbolQuotes.put(indicator.getIndicatorId(), quotesList);
				buffer.put(item.getSymbol(), symbolQuotes);
			}
			indicatorQuote = indicator.execute(quotesList, item);
			if (indicatorQuote != null) {
				result.put(indicator.getIndicatorId(), indicatorQuote);
			}
		}
		//If no indicator computed for the item, return null to skip writing out this item
		//return indicatorQuotes.size() > 0 ? indicatorQuotes : null;
		return result;
	}

	public void setIndicatorFactDao(IndicatorFactDao dao) {
		this.indicatorFactDao = dao;
	}

	public void setIndicatorDimDao(IndicatorsDao indicatorDimDao) {
		this.indicatorDimDao = indicatorDimDao;
	}

}
